package com.mego.acciotweb.utils;

//import java.security.Security;

import java.io.IOException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class TripleDesHelper {
    private static final String Algorithm = "DESede"; // define the encode Algorithm ,can use the
    // DES,DESede,Blowfish
    private static final byte[] DEFAULT_KEY = { (byte) 0xCC, 0x22, 0x4F, 0x58,
            (byte) 0x88, 0x34, 0x28, 0x25, 0x35, 0x79, 0x51, (byte) 0xCB, 0x30,
            0x40, 0x36, 0x28, 0x35, 0x29, 0x11, 0x2B, 0x40, (byte) 0xA8, 0x76,
            0x68 }; // 24byte key

    // keybyte is the encode key ,it's length is 24byte
    // src is the encoded data buffer area |src为被加密的数据缓冲区（源）
    public byte[] encryptMode(byte[] keybyte, byte[] src) {
        try {
            // create the key
            //DESede/CBC/PKCS5Padding --
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            // encode DESede/ECB/PKCS5Padding的简写 是 DESede
            Cipher c1 = Cipher.getInstance("DESede/ECB/PKCS5Padding");
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (Exception e) {
//			AppLoggerUtils.getLogger("TripleDesHelper").error("encryptMode ", e);
            return null;
        }
    }

    // keybyte is the encode key ,it's length is 24byte
    //src is the encoded data buffer area
    public byte[] decryptMode(byte[] keybyte, byte[] src) {
        try {
            // create the key
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

            // decode
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (Exception e) {
//			AppLoggerUtils.getLogger("TripleDesHelper").error("decryptMode ", e);
            return null;
        }
    }

    // convert to the hex string
    public static String byte2ReadableHex(byte[] b, int len) {
        StringBuffer hs = new StringBuffer();
        String stmp = "";

        for (int n = 0; n < len; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs.append("0").append(stmp);
            else
                hs.append(stmp);
            hs.append(" ");
        }
        stmp = hs.toString().toUpperCase();
        return stmp;
    }

    // byte to hex strings
    public static String byte2hex(byte[] b, int len) {
        StringBuffer hs = new StringBuffer();
        String stmp = "";

        for (int n = 0; n < len; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs.append("0").append(stmp);
            else
                hs.append(stmp);
        }
        stmp = hs.toString().toUpperCase();
        return stmp;
    }

    public static byte[] hex2byte(String hex) {
        int cnt = hex.length() / 2;
        byte[] ret = new byte[cnt];
        for (int i = 0; i < ret.length; ++i) {
            String h = hex.substring(i * 2, i * 2 + 2);
            int x = Integer.parseInt(h, 16);
            if (x > 128) {
                x = x - 256;
            }
            ret[i] = (byte) x;
        }
        return ret;
    }


    public static String decrypt(String line, byte[] key) {
        TripleDesHelper h = new TripleDesHelper();
        byte[] bb = h.decryptMode(key, hex2byte(line));
        return new String(bb);
    }

    public static String encrypt(String line, byte[] key) {
        TripleDesHelper h = new TripleDesHelper();
        byte[] bb = h.encryptMode(key, line.getBytes());
        return byte2hex(bb, bb.length);
    }

    public static String defaultEncrypt(String line) {
        String mm = "";
        try {
            mm = new String(DEFAULT_KEY, "utf-8");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return encrypt(line, DEFAULT_KEY);
    }

    public static String defaultDecrypt(String line) {
        return decrypt(line, DEFAULT_KEY);
    }
}
